﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Text;
using Top.Api.Domain;

namespace Newsell
{
    /// <summary>
    /// getData 的摘要说明
    /// </summary>
    public class getData : IHttpHandler, IRequiresSessionState
    {
        ITopControl itc = new ITopControl();
        Boolean isSucsess = true;
        string errMsg;
        string act;

        public void ProcessRequest(HttpContext context)
        {
            act = context.Request.QueryString["act"];

            context.Response.ContentType = "text/plain";
            switch (act)
            {
                case "tree":
                    string treeNode = getSellCats();
                    context.Response.Write(treeNode);
                    context.Response.End();
                    break;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public string getSellCats() 
        {
            itc.sellcatGet(HttpContext.Current.Session["usNick"].ToString());
            List<SellerCat> childlist = new List<SellerCat>();
            if (itc.sclistres.SellerCats.Count != 0)
            {
                StringBuilder sbnode = new StringBuilder();
                sbnode.Append("[");
                //sbnode.Append("{id:'" + itc.sclistres.SellerCats[0].Cid.ToString() + "', pId:'0',isParent:true,name:'" + itc.sclistres.SellerCats[0].Name.ToString() + "'},");
                for (int i = 0; i < itc.sclistres.SellerCats.Count; i++)
                {
                    if (itc.sclistres.SellerCats[i].ParentCid == 0) { sbnode.Append("{id:'" + itc.sclistres.SellerCats[i].Cid.ToString() + "', pId:'0',isParent:true,name:'" + itc.sclistres.SellerCats[i].Name.ToString() + "'},"); }
                    else { sbnode.Append("{id:'" + itc.sclistres.SellerCats[i].Cid.ToString() + "', pId:'" + itc.sclistres.SellerCats[i].ParentCid.ToString() + "', name:'" + itc.sclistres.SellerCats[i].Name.ToString() + "'},"); }
                }
                sbnode.Remove(sbnode.Length - 1, 1);
                sbnode.Append("]");
                return sbnode.ToString();
            }
            else{ return null; }

            //StringBuilder sb = new StringBuilder();
            //sb.Append("<script language='javascript'>");
            //sb.Append(sbnode.ToString());
            //sb.Append("$(document).ready(function () { zTree = $('#tree').zTree(setting, zNodes);refushtreenode() });"); //此处写JS代码
            //sb.Append("</script>");

            //RegisterClientScriptBlock(this.GetType(), "CreateTree", sb.ToString());
        }
    }
}